16. 多个测量值
多个测量值
Question:
Start Quiz:
#Modify the code so that it updates the probability twice
#and gives the posterior distribution after both
#measurements are incorporated. Make sure that your code
#allows for any sequence of measurement of any length.
p=[0.2, 0.2, 0.2, 0.2, 0.2]
world=['green', 'red', 'red', 'green', 'green']
measurements = ['red', 'green']
pHit = 0.6
pMiss = 0.2
def sense(p, Z):
q=[]
for i in range(len(p)):
hit = (Z == world[i])
q.append(p[i] * (hit * pHit + (1-hit) * pMiss))
s = sum(q)
for i in range(len(q)):
q[i] = q[i] / s
return q
#
#ADD YOUR CODE HERE
#
print p
Solution:
INSTRUCTOR NOTE:
切勿修改感应函数。添加代码,以保证 p 是在两次测量之后的正确概率。确保你的代码适用于任意长度的测量列表。